package net.oschina.app.fragment.tweetsfragments;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.R;
import net.oschina.app.adapter.tweetsadapter.TweetsListAdapter;
import net.oschina.app.bean.Tweet;
import net.oschina.app.bean.TweetsList;
import net.oschina.app.constant.Constants;
import net.oschina.app.fragment.BaseFragment;
import net.oschina.app.util.URLGet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ldl on 2017/4/1.
 */

public class HotFragment extends BaseFragment {
    private XRecyclerView mXRecyclerView;
    private List<Tweet> mTweetslist = new ArrayList();
    private TweetsListAdapter mTweetsListAdapter;
    private Handler handler = new Handler();
    private View mView;

    @Override
    protected View initView() {
        // 找到XRecyclerView控件，充气条目在适配器设置
        mView = View.inflate(getContext(), R.layout.base_tweets_fragment, null);
        mXRecyclerView = (XRecyclerView) mView.findViewById(R.id.xrv_base);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mXRecyclerView.setLayoutManager(layoutManager);
        return mView;
    }

    @Override
    protected void setData() {
        String path = Constants.HOST+"/list/tweet_list/page0.xml";
        URLGet urlGet = new URLGet();
        urlGet.getData(path, TweetsList.class, new URLGet.OnURLGetLitener<TweetsList>() {
            @Override
            public void showSuccess(TweetsList bean) {
                mTweetslist = bean.getTweetslist();
                dealCommonSuccess(mTweetslist);
            }

            @Override
            public void showError(Exception e) {

            }
        });
    }

    @Override
    protected void showSuccess() {
        mTweetsListAdapter = new TweetsListAdapter(mTweetslist);
        mXRecyclerView.setAdapter(mTweetsListAdapter);
        changeView(mView);
    }
}